
<!doctype html>
<html lang="zh" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      
      
      <link rel="icon" href="../../assets/images/favicon.png">
      <meta name="generator" content="mkdocs-1.5.3, mkdocs-material-9.1.15">
    
    
      
        <title>实验步骤 - 计算机组成原理实验（2024春季） | 哈工大（深圳）</title>
      
    
    
      <link rel="stylesheet" href="../../assets/stylesheets/main.26e3688c.min.css">
      
        
        <link rel="stylesheet" href="../../assets/stylesheets/palette.ecc896b0.min.css">
      
      

    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
    <script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="orange" data-md-color-accent="orange">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#_1" class="md-skip">
          跳转至
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="页眉">
    <a href="../.." title="计算机组成原理实验（2024春季） | 哈工大（深圳）" class="md-header__button md-logo" aria-label="计算机组成原理实验（2024春季） | 哈工大（深圳）" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M384 96v224H64V96h320zM64 32C28.7 32 0 60.7 0 96v224c0 35.3 28.7 64 64 64h117.3l-10.7 32H96c-17.7 0-32 14.3-32 32s14.3 32 32 32h256c17.7 0 32-14.3 32-32s-14.3-32-32-32h-74.7l-10.7-32H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm464 0c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h64c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48h-64zm16 64h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16s7.2-16 16-16zm-16 80c0-8.8 7.2-16 16-16h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16zm32 160a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2Z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            计算机组成原理实验（2024春季） | 哈工大（深圳）
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              实验步骤
            
          </span>
        </div>
      </div>
    </div>
    
      
        <form class="md-header__option" data-md-component="palette">
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="default" data-md-color-primary="orange" data-md-color-accent="orange"  aria-label="切换到夜晚模式"  type="radio" name="__palette" id="__palette_1">
            
              <label class="md-header__button md-icon" title="切换到夜晚模式" for="__palette_2" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 7a5 5 0 0 1 5 5 5 5 0 0 1-5 5 5 5 0 0 1-5-5 5 5 0 0 1 5-5m0 2a3 3 0 0 0-3 3 3 3 0 0 0 3 3 3 3 0 0 0 3-3 3 3 0 0 0-3-3m0-7 2.39 3.42C13.65 5.15 12.84 5 12 5c-.84 0-1.65.15-2.39.42L12 2M3.34 7l4.16-.35A7.2 7.2 0 0 0 5.94 8.5c-.44.74-.69 1.5-.83 2.29L3.34 7m.02 10 1.76-3.77a7.131 7.131 0 0 0 2.38 4.14L3.36 17M20.65 7l-1.77 3.79a7.023 7.023 0 0 0-2.38-4.15l4.15.36m-.01 10-4.14.36c.59-.51 1.12-1.14 1.54-1.86.42-.73.69-1.5.83-2.29L20.64 17M12 22l-2.41-3.44c.74.27 1.55.44 2.41.44.82 0 1.63-.17 2.37-.44L12 22Z"/></svg>
              </label>
            
          
            
            
            
            <input class="md-option" data-md-color-media="" data-md-color-scheme="slate" data-md-color-primary="orange" data-md-color-accent="orange"  aria-label="切换到白天模式"  type="radio" name="__palette" id="__palette_2">
            
              <label class="md-header__button md-icon" title="切换到白天模式" for="__palette_1" hidden>
                <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m17.75 4.09-2.53 1.94.91 3.06-2.63-1.81-2.63 1.81.91-3.06-2.53-1.94L12.44 4l1.06-3 1.06 3 3.19.09m3.5 6.91-1.64 1.25.59 1.98-1.7-1.17-1.7 1.17.59-1.98L15.75 11l2.06-.05L18.5 9l.69 1.95 2.06.05m-2.28 4.95c.83-.08 1.72 1.1 1.19 1.85-.32.45-.66.87-1.08 1.27C15.17 23 8.84 23 4.94 19.07c-3.91-3.9-3.91-10.24 0-14.14.4-.4.82-.76 1.27-1.08.75-.53 1.93.36 1.85 1.19-.27 2.86.69 5.83 2.89 8.02a9.96 9.96 0 0 0 8.02 2.89m-1.64 2.02a12.08 12.08 0 0 1-7.8-3.47c-2.17-2.19-3.33-5-3.49-7.82-2.81 3.14-2.7 7.96.31 10.98 3.02 3.01 7.84 3.12 10.98.31Z"/></svg>
              </label>
            
          
        </form>
      
    
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="搜索" placeholder="搜索" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12Z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="查找">
        
        <button type="reset" class="md-search__icon md-icon" title="清空当前内容" aria-label="清空当前内容" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41Z"/></svg>
        </button>
      </nav>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            正在初始化搜索引擎
          </div>
          <ol class="md-search-result__list" role="presentation"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
      <div class="md-header__source">
        <a href="https://gitee.com/hitsz-cslab/organ" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    HITSZ-COMP2008
  </div>
</a>
      </div>
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
            
<nav class="md-tabs" aria-label="标签" data-md-component="tabs">
  <div class="md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../.." class="md-tabs__link">
        实验须知
      </a>
    </li>
  

      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../0-overview/" class="md-tabs__link">
        实验1 从C语言到机器码
      </a>
    </li>
  

      
    </ul>
  </div>
</nav>
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


<nav class="md-nav md-nav--primary md-nav--lifted" aria-label="导航栏" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../.." title="计算机组成原理实验（2024春季） | 哈工大（深圳）" class="md-nav__button md-logo" aria-label="计算机组成原理实验（2024春季） | 哈工大（深圳）" data-md-component="logo">
      
  
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M384 96v224H64V96h320zM64 32C28.7 32 0 60.7 0 96v224c0 35.3 28.7 64 64 64h117.3l-10.7 32H96c-17.7 0-32 14.3-32 32s14.3 32 32 32h256c17.7 0 32-14.3 32-32s-14.3-32-32-32h-74.7l-10.7-32H384c35.3 0 64-28.7 64-64V96c0-35.3-28.7-64-64-64H64zm464 0c-26.5 0-48 21.5-48 48v352c0 26.5 21.5 48 48 48h64c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48h-64zm16 64h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16s7.2-16 16-16zm-16 80c0-8.8 7.2-16 16-16h32c8.8 0 16 7.2 16 16s-7.2 16-16 16h-32c-8.8 0-16-7.2-16-16zm32 160a32 32 0 1 1 0 64 32 32 0 1 1 0-64z"/></svg>

    </a>
    计算机组成原理实验（2024春季） | 哈工大（深圳）
  </label>
  
    <div class="md-nav__source">
      <a href="https://gitee.com/hitsz-cslab/organ" title="前往仓库" class="md-source" data-md-component="source">
  <div class="md-source__icon md-icon">
    
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.4.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2023 Fonticons, Inc.--><path d="M439.55 236.05 244 40.45a28.87 28.87 0 0 0-40.81 0l-40.66 40.63 51.52 51.52c27.06-9.14 52.68 16.77 43.39 43.68l49.66 49.66c34.23-11.8 61.18 31 35.47 56.69-26.49 26.49-70.21-2.87-56-37.34L240.22 199v121.85c25.3 12.54 22.26 41.85 9.08 55a34.34 34.34 0 0 1-48.55 0c-17.57-17.6-11.07-46.91 11.25-56v-123c-20.8-8.51-24.6-30.74-18.64-45L142.57 101 8.45 235.14a28.86 28.86 0 0 0 0 40.81l195.61 195.6a28.86 28.86 0 0 0 40.8 0l194.69-194.69a28.86 28.86 0 0 0 0-40.81z"/></svg>
  </div>
  <div class="md-source__repository">
    HITSZ-COMP2008
  </div>
</a>
    </div>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_1" >
      
      
      
        <label class="md-nav__link" for="__nav_1" id="__nav_1_label" tabindex="0">
          实验须知
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_1_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_1">
          <span class="md-nav__icon md-icon"></span>
          实验须知
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../.." class="md-nav__link">
        实验介绍
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_1_2" >
      
      
      
        <label class="md-nav__link" for="__nav_1_2" id="__nav_1_2_label" tabindex="0">
          Verilog语法回顾
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_1_2_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_1_2">
          <span class="md-nav__icon md-icon"></span>
          Verilog语法回顾
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part0/" class="md-nav__link">
        初学Verilog的几点提醒
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part1/" class="md-nav__link">
        模块的基本结构
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part2/" class="md-nav__link">
        标识符和数据类型
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part3/" class="md-nav__link">
        运算符及表达式
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part4/" class="md-nav__link">
        逻辑门的描述
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part5/" class="md-nav__link">
        赋值语句和块语句
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part6/" class="md-nav__link">
        条件语句和循环语句
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/defparam/" class="md-nav__link">
        宏定义和参数
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part7/" class="md-nav__link">
        模块的实例化
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/module_test/" class="md-nav__link">
        模块的测试
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/verilog/part8/" class="md-nav__link">
        4位全加器设计实例
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../home/operation/" class="md-nav__link">
        实验提交说明
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2" >
      
      
      
        <label class="md-nav__link" for="__nav_2" id="__nav_2_label" tabindex="0">
          实验1 从C语言到机器码
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="1" aria-labelledby="__nav_2_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2">
          <span class="md-nav__icon md-icon"></span>
          实验1 从C语言到机器码
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../0-overview/" class="md-nav__link">
        实验概述
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../1-theory/" class="md-nav__link">
        实验原理
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2_3" >
      
      
      
        <label class="md-nav__link" for="__nav_2_3" id="__nav_2_3_label" tabindex="0">
          实验步骤
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_2_3_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2_3">
          <span class="md-nav__icon md-icon"></span>
          实验步骤
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../2-step0/" class="md-nav__link">
        开始之前
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../2-step1/" class="md-nav__link">
        C语言到汇编
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../2-step2/" class="md-nav__link">
        编写汇编程序
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../3-extra/" class="md-nav__link">
        附加题
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../4-submit/" class="md-nav__link">
        验收与提交
      </a>
    </li>
  

            
          
            
              
  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
      
      
        
      
      <input class="md-nav__toggle md-toggle md-toggle--indeterminate" type="checkbox" id="__nav_2_6" >
      
      
      
        <label class="md-nav__link" for="__nav_2_6" id="__nav_2_6_label" tabindex="0">
          附录
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" data-md-level="2" aria-labelledby="__nav_2_6_label" aria-expanded="false">
        <label class="md-nav__title" for="__nav_2_6">
          <span class="md-nav__icon md-icon"></span>
          附录
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../A-envir/" class="md-nav__link">
        A. 实验环境安装
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../B-lnxcmd/" class="md-nav__link">
        B. Linux常用命令
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../C-rv_ref/" class="md-nav__link">
        C. RISC-V参考资料
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
              
              <div class="md-sidebar md-sidebar--secondary" data-md-component="sidebar" data-md-type="toc" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

<nav class="md-nav md-nav--secondary" aria-label="目录">
  
  
  
    
  
  
    <label class="md-nav__title" for="__toc">
      <span class="md-nav__icon md-icon"></span>
      目录
    </label>
    <ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
      
        <li class="md-nav__item">
  <a href="#0" class="md-nav__link">
    0. 开始之前
  </a>
  
    <nav class="md-nav" aria-label="0. 开始之前">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#01" class="md-nav__link">
    0.1 如何使用虚拟机？
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#02" class="md-nav__link">
    0.2 如何访问虚拟机文件？
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#03" class="md-nav__link">
    0.3 创建实验目录
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#1-c" class="md-nav__link">
    1. C语言到汇编
  </a>
  
    <nav class="md-nav" aria-label="1. C语言到汇编">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#11" class="md-nav__link">
    1.1 编写样例程序
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#12" class="md-nav__link">
    1.2 预编译
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#13" class="md-nav__link">
    1.3 编译
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#14" class="md-nav__link">
    1.4 汇编
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#15" class="md-nav__link">
    1.5 生成可执行文件
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#16" class="md-nav__link">
    1.6 反汇编
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
        <li class="md-nav__item">
  <a href="#2" class="md-nav__link">
    2. 编写汇编程序
  </a>
  
    <nav class="md-nav" aria-label="2. 编写汇编程序">
      <ul class="md-nav__list">
        
          <li class="md-nav__item">
  <a href="#21-rars" class="md-nav__link">
    2.1 RARS操作流程
  </a>
  
</li>
        
          <li class="md-nav__item">
  <a href="#22" class="md-nav__link">
    2.2 数据段访问方法
  </a>
  
</li>
        
      </ul>
    </nav>
  
</li>
      
    </ul>
  
</nav>
                  </div>
                </div>
              </div>
            
          
          
            <div class="md-content" data-md-component="content">
              <article class="md-content__inner md-typeset">
                
                  


<h1 id="_1">实验步骤</h1>
<h2 id="0">0. 开始之前</h2>
<p>&emsp;&emsp;首先，简要介绍基于WSL（<a href="https://docs.microsoft.com/windows/wsl" target="_blank">Windows Subsystem for Linux</a>）的虚拟机使用方法。</p>
<h3 id="01">0.1 如何使用虚拟机？</h3>
<div class="tabbed-set tabbed-alternate" data-tabs="1:2"><input checked="checked" id="__tabbed_1_1" name="__tabbed_1" type="radio" /><input id="__tabbed_1_2" name="__tabbed_1" type="radio" /><div class="tabbed-labels"><label for="__tabbed_1_1">612、615实验室</label><label for="__tabbed_1_2">506实验室</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<p>&emsp;&emsp;在 <strong>612、615实验室</strong> 上课的同学，在桌面上找到Debian虚拟机的快捷方式，双击启动Debian虚拟机，如图3-1所示。</p>
<p><center><img src="../assets/3-1-1.png" width = 400></center>
<center>图3-1 启动虚拟机</center></p>
</div>
<div class="tabbed-block">
<p>&emsp;&emsp;在 <strong>506实验室</strong> 上课的同学，在桌面、开始屏幕或任务栏搜索框找到并打开wsl2distromanager，点击按钮启动comp2008虚拟机，如图3-1所示。</p>
<p><center><img src="../assets/3-1.png" width = 650></center>
<center>图3-1 启动虚拟机</center></p>
</div>
</div>
</div>
<p>&emsp;&emsp;启动虚拟机后，可在终端窗口中输入并执行命令。虚拟机的 <span style="background-color: #00FFFF;"><strong>用户密码、root密码均为123</strong></span>。</p>
<h3 id="02">0.2 如何访问虚拟机文件？</h3>
<div class="tabbed-set tabbed-alternate" data-tabs="2:2"><input checked="checked" id="__tabbed_2_1" name="__tabbed_2" type="radio" /><input id="__tabbed_2_2" name="__tabbed_2" type="radio" /><div class="tabbed-labels"><label for="__tabbed_2_1">612、615实验室</label><label for="__tabbed_2_2">506实验室</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<p>&emsp;&emsp;在 <strong>612、615实验室</strong> 上课的同学，在桌面上找到<code>rootfs</code>文件夹的快捷方式，双击打开，即可访问虚拟机的文件系统，如图3-2所示。</p>
<p><center><img src="../assets/3-2.png" width = 700></center>
<center>图3-2 访问虚拟机文件系统</center></p>
<p>&emsp;&emsp;我们可以在图3-2所示的资源管理器窗口中直接访问虚拟机的文件夹及文件。双击进入相应目录，即可直接对虚拟机的文件进行访问、创建、编辑、复制等操作。</p>
</div>
<div class="tabbed-block">
<p>&emsp;&emsp;在 <strong>506实验室</strong> 上课的同学，通过快捷键 <span class="keys"><kbd class="key-windows">Win</kbd><span>+</span><kbd class="key-e">E</kbd></span> 打开文件资源管理器，然后在地址栏输入 <code>\\wsl$</code> 并回车，或点击左下方的Linux，即可访问虚拟机文件系统，如图3-2所示。</p>
<p><center><img src="../assets/a-2.png" width = 600></center>
<center>图3-2 访问虚拟机文件系统</center></p>
<p>&emsp;&emsp;在图3-2中，每个文件夹均代表一个虚拟机的文件系统，且文件夹与虚拟机同名。双击进入相应的虚拟机文件系统后，即可直接对虚拟机的文件进行访问、创建、编辑、复制等操作。</p>
</div>
</div>
</div>
<div class="admonition example">
<p class="admonition-title">重要说明 <img alt="📝" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4dd.svg" title=":pencil:" /></p>
<p>&emsp;&emsp;要想虚拟机正常使用Windows下创建的文件夹，需在虚拟机中执行<code>sudo chmod -R 777 &lt;文件夹路径+文件夹名称&gt;</code>将其访问权限修改为<code>777</code>，详见<a href="../B-lnxcmd" target="_blank">附录B第11点</a>。</p>
<p>&emsp;&emsp;类似地，要想虚拟机正常使用Windows下创建的文件，需在虚拟机中执行<code>sudo chmod 777 &lt;文件路径+文件名称&gt;</code>。</p>
</div>
<h3 id="03">0.3 创建实验目录</h3>
<div class="tabbed-set tabbed-alternate" data-tabs="3:2"><input checked="checked" id="__tabbed_3_1" name="__tabbed_3" type="radio" /><input id="__tabbed_3_2" name="__tabbed_3" type="radio" /><div class="tabbed-labels"><label for="__tabbed_3_1">612、615实验室</label><label for="__tabbed_3_2">506实验室</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<p>&emsp;&emsp;在 <strong>612、615实验室</strong> 上课的同学，双击进入图3-2所示的<code>home/</code>目录，然后继续双击进入其中的<code>usr/</code>目录，如图3-3所示。</p>
<p><center><img src="../assets/3-3-1.png"></center>
<center>图3-3 双击进入用户目录</center></p>
<p>&emsp;&emsp;在图3-3所示的用户目录中，右键新建文件夹，命名为<code>lab1</code>。随后 <strong>关闭虚拟机窗口并重新打开</strong>，然后在虚拟机终端内输入<code>sudo chmod -R 777 lab1</code>命令，回车并输入root密码。注意 <strong>不要改动或删除已有文件</strong>。</p>
</div>
<div class="tabbed-block">
<p>&emsp;&emsp;在 <strong>506实验室</strong> 上课的同学，双击进入图3-2所示的<code>comp2008</code>文件夹，然后再依次双击进入<code>/home/usr/</code>的用户目录，或在地址栏直接访问<code>\\wsl$\comp2008\home\usr</code>，如图3-3所示。</p>
<p><center><img src="../assets/3-3.png"></center>
<center>图3-3 双击进入用户目录</center></p>
<p>&emsp;&emsp;在图3-3所示的用户目录中，右键新建文件夹，命名为<code>lab1</code>。随后在虚拟机终端内输入<code>sudo chmod -R 777 lab1</code>命令，回车并输入root密码。注意 <strong>不要改动或删除已有文件</strong>。</p>
</div>
</div>
</div>
<p>&emsp;&emsp;创建目录时，也可在虚拟机终端内直接执行<code>mkdir lab1</code>命令。</p>
<p>&emsp;&emsp;</p>
<h2 id="1-c">1. C语言到汇编</h2>
<p>&emsp;&emsp;下面用一个简单的C程序为例，简单说明C程序从编译到执行的过程。</p>
<h3 id="11">1.1 编写样例程序</h3>
<p>&emsp;&emsp;打开<code>lab1</code>文件夹，新建两个文本文件，将其分别重命名为<code>main.c</code>和<code>func.h</code>。</p>
<div class="admonition warning">
<p class="admonition-title">小提醒 <img alt="📢" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4e2.svg" title=":loudspeaker:" /></p>
<p>&emsp;&emsp;在Windows下重命名文件时，首先在文件资源管理器中，打开 “查看” -&gt; “文件扩展名” 选项，如下图所示。</p>
<p><center><img src="../assets/3-x1.png" width = 100%></center></p>
</div>
<p>&emsp;&emsp;点击下列代码右上角的复制按钮，将示例程序分别粘贴到<code>main.c</code>和<code>func.h</code>。该程序的功能是字符串匹配。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="4:2"><input checked="checked" id="__tabbed_4_1" name="__tabbed_4" type="radio" /><input id="__tabbed_4_2" name="__tabbed_4" type="radio" /><div class="tabbed-labels"><label for="__tabbed_4_1">main.c</label><label for="__tabbed_4_2">func.h</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="c1">// #include &lt;stdio.h&gt;</span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;func.h&quot;</span><span class="c1">   // (1)!</span>

<span class="cp">#define STR_A   &quot;1qab9a0bcabcds13&quot;  </span><span class="c1">// (2)!</span>
<span class="cp">#define STR_B   &quot;bcds&quot;  </span><span class="c1">// (3)!</span>

<span class="kt">int</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">find_substr</span><span class="p">(</span><span class="n">STR_A</span><span class="p">,</span><span class="w"> </span><span class="n">STR_B</span><span class="p">,</span><span class="w"> </span><span class="mi">16</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">);</span><span class="w">     </span><span class="c1">// (4)!</span>
<span class="w">    </span><span class="cm">/* printf(&quot;%d\n&quot;, pos); */</span>

<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">pos</span><span class="p">;</span><span class="w">     </span><span class="c1">// (5)!</span>
<span class="p">}</span>
</code></pre></div></td></tr></table></div>
<ol>
<li>包含头文件</li>
<li>宏定义</li>
<li>宏定义</li>
<li>使用宏定义；调用头文件中的函数</li>
<li>将找到的子串位置返回</li>
</ol>
</div>
<div class="tabbed-block">
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span>
<span class="normal">12</span>
<span class="normal">13</span>
<span class="normal">14</span>
<span class="normal">15</span>
<span class="normal">16</span>
<span class="normal">17</span>
<span class="normal">18</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="kt">int</span><span class="w"> </span><span class="nf">find_substr</span><span class="p">(</span><span class="kt">char</span><span class="o">*</span><span class="w"> </span><span class="n">str</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="o">*</span><span class="w"> </span><span class="n">pattern</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">len1</span><span class="p">,</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="n">len2</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">-1</span><span class="p">;</span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">len1</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span>
<span class="w">    </span><span class="p">{</span>
<span class="w">        </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">len2</span><span class="p">;</span><span class="w"> </span><span class="n">j</span><span class="o">++</span><span class="p">)</span>
<span class="w">            </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">str</span><span class="p">[</span><span class="n">i</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="n">j</span><span class="p">]</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="n">pattern</span><span class="p">[</span><span class="n">j</span><span class="p">])</span>
<span class="w">                </span><span class="k">break</span><span class="p">;</span>
<span class="w">            </span><span class="k">else</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">j</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">len2</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span>
<span class="w">            </span><span class="p">{</span>
<span class="w">                </span><span class="n">pos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">i</span><span class="p">;</span>
<span class="w">                </span><span class="k">break</span><span class="p">;</span>
<span class="w">            </span><span class="p">}</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">pos</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">-1</span><span class="p">)</span><span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">pos</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></td></tr></table></div>
</div>
</div>
</div>
<p>&emsp;&emsp;在<code>main.c</code>中，</p>
<ul>
<li>第1、10行含有注释；  </li>
<li>第2行包含了头文件；  </li>
<li>第4、5行定义了宏定义；  </li>
<li>第9行使用了宏定义，并调用了头文件内的函数。</li>
</ul>
<p>&emsp;&emsp;请关注下一步预编译后，这些地方都发生了什么变化。</p>
<h3 id="12">1.2 预编译</h3>
<p>&emsp;&emsp;执行<code>cd lab1</code>命令以进入<code>lab1</code>文件夹，然后执行预编译命令<code>riscv32-unknown-elf-gcc -E main.c -o main.i</code>，从而生成预编译文件<code>main.i</code>，如图3-4所示。</p>
<p><center><img src="../assets/3-4.png" width = 600></center>
<center>图3-4 预编译</center></p>
<div class="admonition warning">
<p class="admonition-title">小提醒 <img alt="📢" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4e2.svg" title=":loudspeaker:" /></p>
<p>&emsp;&emsp;在612、615上课的同学，如果在Windows下给虚拟机新建了文件，但在虚拟机终端执行以上命令时，报错提示找不到文件，则 <strong>关闭虚拟机窗口并重新打开</strong> 后再重试。</p>
</div>
<p>&emsp;&emsp;在Windows下访问<code>lab1</code>文件夹，双击打开并查看预编译文件<code>main.i</code>，将其与源文件作对比，如图3-5所示。</p>
<div class="tabbed-set tabbed-alternate" data-tabs="5:3"><input checked="checked" id="__tabbed_5_1" name="__tabbed_5" type="radio" /><input id="__tabbed_5_2" name="__tabbed_5" type="radio" /><input id="__tabbed_5_3" name="__tabbed_5" type="radio" /><div class="tabbed-labels"><label for="__tabbed_5_1">对比观察注释</label><label for="__tabbed_5_2">对比观察头文件</label><label for="__tabbed_5_3">对比观察宏定义</label></div>
<div class="tabbed-content">
<div class="tabbed-block">
<p><center><img src="../assets/3-5a.png" width = 100%></center></p>
</div>
<div class="tabbed-block">
<p><center><img src="../assets/3-5b.png" width = 100%></center></p>
</div>
<div class="tabbed-block">
<p><center><img src="../assets/3-5c.png" width = 100%></center></p>
</div>
</div>
</div>
<p><center>图3-5 对比预编译文件与源文件</center></p>
<h3 id="13">1.3 编译</h3>
<p>&emsp;&emsp;在终端内执行命令<code>riscv32-unknown-elf-gcc -S main.i -o main.s</code>，从而编译预编译文件<code>main.i</code>，生成汇编文件<code>main.s</code>。</p>
<p>&emsp;&emsp;在Windows下双击打开并查看汇编文件<code>main.s</code>，将其与预编译文件<code>main.i</code>作对比，如图3-6所示。<strong>注意观察汇编程序中，函数（子程序）内部的执行流程</strong>。</p>
<p><center><img src="../assets/3-6.png" width = 100%></center>
<center>图3-6 对比汇编文件和预编译文件</center></p>
<div class="admonition tip">
<p class="admonition-title">注解1：子程序流程 <img alt="📝" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4dd.svg" title=":pencil:" /></p>
<p>&emsp;&emsp;图3-6是一个典型的使用函数（子程序）的汇编程序例子，可知子程序的流程为：<br />
<pre class="mermaid"><code>graph LR
A[开始] --&gt; B[&lt;font color=#FF00FF&gt;&lt;b&gt;&lt;u&gt;保护现场&lt;/u&gt;&lt;/b&gt;&lt;/font&gt;];
B --&gt; C[取出参数];
C --&gt; D[处理参数];
D --&gt; E[保存返回值];
E --&gt; F[&lt;font color=#FF00FF&gt;&lt;b&gt;&lt;u&gt;恢复现场&lt;/u&gt;&lt;/b&gt;&lt;/font&gt;];
F --&gt; G[函数返回];
G --&gt; H[结束];</code></pre></p>
</div>
<div class="admonition tip">
<p class="admonition-title">注解2：子程序传参 <img alt="📝" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4dd.svg" title=":pencil:" /></p>
<p>&emsp;&emsp;在RISC-V汇编语言编程规范中，<code>a0</code>-<code>a7</code>寄存器通常在函数（子程序）调用时用于存放和传递整型参数。其中，<code>a0</code>、<code>a1</code>还可用于传递函数（子程序）的返回值。因此，观察图3-6右侧<code>main.s</code>第43-48行可以发现，<code>main()</code>在调用<code>find_substr()</code>前，将4个参数分别保存到了<code>a0</code>-<code>a3</code>寄存器当中；而<code>main()</code>和<code>find_substr()</code>在返回前，都将返回值保存到了<code>a0</code>寄存器。</p>
<p>&emsp;&emsp;需要注意的是，图3-6右侧的汇编程序是由编译器生成的。编译器需要考虑函数调用规范、指令集规范等多方面因素，因此生成的汇编代码看起来可能存在一些“多余”的操作。比如在<code>find_substr()</code>中，明明参数已经存放在<code>a0</code>-<code>a3</code>寄存器，可以直接使用了，但偏偏要先把它们压栈，然后使用时再从栈里取出。实际上，我们在自己编写子程序时，可以直接使用<code>a0</code>-<code>a3</code>寄存器，不需像编译器这般加入额外的压栈和弹栈操作。</p>
</div>
<h3 id="14">1.4 汇编</h3>
<p>&emsp;&emsp;在终端内执行命令<code>riscv32-unknown-elf-gcc -c main.s -o main.o</code>，从而对汇编文件<code>main.s</code>进行汇编，得到ELF格式的目标文件<code>main.o</code>。</p>
<p>&emsp;&emsp;在终端内执行命令<code>riscv32-unknown-elf-objdump -d main.o &gt; main.o.txt</code>可将目标文件的机器码保存到<code>main.o.txt</code>文件中。</p>
<div class="admonition info">
<p class="admonition-title">关于重定向符 <code>&gt;</code> <img alt="📚" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4da.svg" title=":books:" /></p>
<p>&emsp;&emsp;<code>&gt;</code>是重定向符号，其作用是将符号左侧命令的输出内容重定向到符号右侧的文件并保存下来。</p>
</div>
<p>&emsp;&emsp;打开<code>main.o.txt</code>，查看目标程序机器码，将其与汇编文件<code>main.s</code>作对比，如图3-7所示。</p>
<p><center><img src="../assets/3-7.png" width = 750></center>
<center>图3-7 查看目标程序的机器码</center></p>
<h3 id="15">1.5 生成可执行文件</h3>
<p>&emsp;&emsp;在终端内执行命令<code>riscv32-unknown-elf-gcc main.c -o main</code>，从而将目标文件<code>main.o</code>和库文件进行链接，得到ELF格式的可执行文件<code>main</code>。</p>
<p>&emsp;&emsp;在终端内执行命令<code>spike --isa=rv32g pk main</code>，即可执行<code>main</code>程序。由于该程序没有<code>printf</code>语句，因此将看不到输出信息。观察<code>main.c</code>的代码，可以发现计算结果<code>c</code>是<code>main</code>函数的返回值。因此，可在终端内执行<code>echo $?</code>查看程序的返回值，如图3-8所示。</p>
<p><center><img src="../assets/3-8.png" width = 500></center>
<center>图3-8 执行<code>main</code>程序并查看其返回值</center></p>
<h3 id="16">1.6 反汇编</h3>
<p>&emsp;&emsp;汇编是指将汇编程序翻译成机器码的过程，反汇编则是其反过程。</p>
<p>&emsp;&emsp;在终端内执行命令<code>riscv32-unknown-elf-objdump -d main &gt; main.txt</code>，即可将可执行文件<code>main</code>的反汇编结果保存到<code>main.txt</code>文件。</p>
<p>&emsp;&emsp;打开<code>main.txt</code>，查看可执行程序的机器码，将其与链接前的目标程序机器码<code>main.o.txt</code>作对比，如图3-9所示。</p>
<p><center><img src="../assets/3-9.png" width = 100%></center>
<center>图3-9 查看反汇编文件</center></p>
<h2 id="2">2. 编写汇编程序</h2>
<h3 id="21-rars">2.1 RARS操作流程</h3>
<p>&emsp;&emsp;（1）<strong>编写程序</strong>：在RARS中，新建<code>.asm</code>文件，并编写汇编程序；也可使用代码编辑器编写汇编程序，保存后再用RARS调试和运行程序；</p>
<p>&emsp;&emsp;（2）<strong>对程序汇编</strong>：点击菜单栏按钮，进行汇编，如图3-10所示；</p>
<p><center><img src="../assets/3-10.png" width = 450></center>
<center>图3-10 点击按钮进行汇编</center></p>
<p>&emsp;&emsp;（3）<strong>运行和调试程序</strong>：点击运行按钮直接运行程序；点击调试按钮可每次执行一条指令，如图3-11所示；</p>
<p><center><img src="../assets/3-11.png" width = 500></center>
<center>图3-11 运行或调试程序</center></p>
<div class="admonition tip">
<p class="admonition-title">调试技巧 <img alt="💡" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f4a1.svg" title=":bulb:" /></p>
<p>&emsp;&emsp;为了提高调试效率，可在RARS中设置断点。具体方法是，在<code>Text Segment</code>窗口的<code>Bkpt</code>列下面，给相应的指令打断点；然后点击运行按钮，即可直接执行到断点处的指令，如图3-12所示。</p>
<p><center><img src="../assets/3-12.png" width = 460></center>
<center>图3-12 设置断点</center></p>
</div>
<p>&emsp;&emsp;（4）<strong>查看运行结果</strong>：在 <u>运行结果区</u>、<u>数据区</u> 和 <u>寄存器区</u> 观察结果，如图3-13所示。</p>
<p><center><img src="../assets/3-13.png" width = 100%></center>
<center>图3-13 查看汇编程序的运行结果</center></p>
<h3 id="22">2.2 数据段访问方法</h3>
<p>&emsp;&emsp;打开RARS的存储配置，如图3-14所示。</p>
<p><center><img src="../assets/3-14.png" width = 100%></center>
<center>图3-14 从RARS菜单打开存储配置</center></p>
<p>&emsp;&emsp;由图3-14右侧的存储配置窗口可知，在RARS的默认配置下，数据段基地址为<code>0x10010000</code>。</p>
<p>&emsp;&emsp;以下给出访问数据段的参考代码：</p>
<div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"> 1</span>
<span class="normal"> 2</span>
<span class="normal"> 3</span>
<span class="normal"> 4</span>
<span class="normal"> 5</span>
<span class="normal"> 6</span>
<span class="normal"> 7</span>
<span class="normal"> 8</span>
<span class="normal"> 9</span>
<span class="normal">10</span>
<span class="normal">11</span></pre></div></td><td class="code"><div><pre><span></span><code><span class="na">.data</span>
<span class="w">    </span><span class="nl">array:</span><span class="w"> </span><span class="na">.word</span><span class="w">   </span><span class="mi">0xA</span><span class="p">,</span><span class="w"> </span><span class="mi">0x123</span><span class="p">,</span><span class="w"> </span><span class="mi">0xFF</span>
<span class="w">    </span><span class="nl">str:</span><span class="w">   </span><span class="na">.string</span><span class="w"> </span><span class="s">&quot;Hello World!\n&quot;</span>

<span class="na">.text</span>
<span class="nl">MAIN:</span>
<span class="w">    </span><span class="nf">lui</span><span class="w">  </span><span class="no">s0</span><span class="p">,</span><span class="w"> </span><span class="mi">0x10010</span><span class="w">     </span><span class="c1"># 将数据段基地址赋值给s0寄存器</span>
<span class="w">    </span><span class="nf">lw</span><span class="w">   </span><span class="no">t0</span><span class="p">,</span><span class="w"> </span><span class="mi">0x0</span><span class="p">(</span><span class="no">s0</span><span class="p">)</span><span class="w">     </span><span class="c1"># 读取array[0]到t0寄存器</span>
<span class="w">    </span><span class="nf">lw</span><span class="w">   </span><span class="no">t1</span><span class="p">,</span><span class="w"> </span><span class="mi">0x4</span><span class="p">(</span><span class="no">s0</span><span class="p">)</span><span class="w">     </span><span class="c1"># 读取array[1]到t1寄存器</span>
<span class="w">    </span><span class="nf">lw</span><span class="w">   </span><span class="no">t2</span><span class="p">,</span><span class="w"> </span><span class="mi">0x8</span><span class="p">(</span><span class="no">s0</span><span class="p">)</span><span class="w">     </span><span class="c1"># 读取array[2]到t2寄存器</span>
<span class="w">    </span><span class="nf">addi</span><span class="w"> </span><span class="no">a0</span><span class="p">,</span><span class="w"> </span><span class="no">s0</span><span class="p">,</span><span class="w"> </span><span class="mi">12</span><span class="w">      </span><span class="c1"># 将字符串str的基地址赋值给a0寄存器  </span>
</code></pre></div></td></tr></table></div>
<p>&emsp;&emsp;在RARS中运行上述程序，可在数据区查看数据段中的变量的值，如图3-15所示。</p>
<p><center><img src="../assets/3-15.png" width = 700></center>
<center>图3-15 在RARS中查看数据段中的变量的值</center></p>
<div class="admonition note">
<p class="admonition-title">关于数据段中的字符串 <img alt="🔬" class="twemoji" src="https://cdn.jsdelivr.net/gh/jdecked/twemoji@14.1.2/assets/svg/1f52c.svg" title=":microscope:" /></p>
<p>&emsp;&emsp;（1）字符串中的每个字符均以ASCII码的形式存储在数据段中；<br />
&emsp;&emsp;（2）数据段中的数据均以小端方式存储。</p>
</div>
<p>&emsp;&emsp;执行程序后，可在RARS右侧查看<code>s0</code>、<code>t0</code>、<code>t1</code>、<code>t2</code>和<code>a0</code>寄存器的值，如图3-16所示。</p>
<p><center><img src="../assets/3-16.png" width = 200></center>
<center>图3-16 查看程序执行结果</center></p>





                
              </article>
            </div>
          
          
  <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var tab,labels=set.querySelector(".tabbed-labels");for(tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script>

        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
    <div class="md-copyright__highlight">
      Copyright &copy; 2020 - 2024 哈尔滨工业大学（深圳）
    </div>
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    
    <script id="__config" type="application/json">{"base": "../..", "features": ["content.code.copy", "content.code.annotate", "content.tabs.link", "navigation.expand", "navigation.tabs"], "search": "../../assets/javascripts/workers/search.208ed371.min.js", "translations": {"clipboard.copied": "\u5df2\u590d\u5236", "clipboard.copy": "\u590d\u5236", "search.result.more.one": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.more.other": "\u5728\u8be5\u9875\u4e0a\u8fd8\u6709 # \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.none": "\u6ca1\u6709\u627e\u5230\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.one": "\u627e\u5230 1 \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.other": "# \u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7ed3\u679c", "search.result.placeholder": "\u952e\u5165\u4ee5\u5f00\u59cb\u641c\u7d22", "search.result.term.missing": "\u7f3a\u5c11", "select.version": "\u9009\u62e9\u5f53\u524d\u7248\u672c"}}</script>
    
    
      <script src="../../assets/javascripts/bundle.b4d07000.min.js"></script>
      
        <script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML"></script>
      
    
  </body>
</html>